package com.lcf.common.web;

import com.lcf.common.model.Constant;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * @author ChenFei
 * @date 2021/10/25
 * <p>
 * rest 接口统一返回实体
 */
@Data
@Accessors(chain = true)
public class Result<T> implements Serializable {

    private Integer code;

    private String message;

    private T data;

    private Long timestamp = System.currentTimeMillis();

    public static <T> Result success() {
        return new Result<>().setCode(Constant.SUCCESS_CODE).setMessage(Constant.SUCCESS);
    }

    public static <T> Result<T> success(T data) {
        return new Result().setCode(Constant.SUCCESS_CODE).setMessage(Constant.SUCCESS).setData(data);
    }

    public static <T> Result<T> success(String message, T data) {
        return new Result().setCode(Constant.SUCCESS_CODE).setMessage(message).setData(data);
    }

    public static <T> Result fail() {
        return new Result<>().setCode(Constant.ERROR_CODE).setMessage(Constant.ERROR);
    }

    public static <T> Result fail(String message) {
        return new Result<>().setCode(Constant.ERROR_CODE).setMessage(message);
    }

    public static <T> Result<T> fail(String message, T data) {
        return new Result().setCode(Constant.ERROR_CODE).setMessage(message).setData(data);
    }

}
